//
//  TextViewController.m
//  Weibo
//
//  Created by wpapple on 16/6/4.
//  Copyright © 2016年 红红火火恍恍惚惚嘿嘿嘿. All rights reserved.
//

#import "TextViewController.h"
#import "Account.h"
#import "PicViewController.h"

@interface TextViewController ()

@property (weak, nonatomic) IBOutlet UITextView *editTextView;

@property (strong , nonatomic) UILabel * placeLabel;

@property (strong , nonatomic) PicViewController * picViewController;

@end

@implementation TextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(placeLabelChange) name:UITextViewTextDidChangeNotification object:nil];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(placeLabelChange) name:UITextViewTextDidEndEditingNotification object:nil];
    
    self.placeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, self.view.width, 20)];
    self.placeLabel.text = @"分享新鲜事...";
    self.placeLabel.textColor = [UIColor grayColor];
    
    [self.editTextView addSubview:self.placeLabel];
}

-(void)placeLabelChange{
    
    if (self.editTextView.text.length != 0) {
        self.placeLabel.alpha = 0;
    }else{
        self.placeLabel.alpha = 1;
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)closeThisController:(UIBarButtonItem *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (IBAction)sendMessage:(UIBarButtonItem *)sender {
    
    if (self.picViewController.dataArray.count > 0) {
        [self sendImage];
    }else{
        [self sendText];
    }
    
    
    
    
    
}

-(void)sendText{
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    NSString * url = @"https://api.weibo.com/2/statuses/update.json";
    
    NSMutableDictionary * parameters = [NSMutableDictionary dictionary];
    
    parameters[@"access_token"] = [Account accountFromSandbox].access_token;
    parameters[@"status"] = self.editTextView.text;
    parameters[@"visible"] = @{@1:@"type"};
    
    [manager POST:url parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"success");
        [self dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error");
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    
    
    
//    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)sendImage{
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    NSString * url = @"https://upload.api.weibo.com/2/statuses/upload.json";
    
    NSMutableDictionary * parameters = [NSMutableDictionary dictionary];
    
    parameters[@"access_token"] = [Account accountFromSandbox].access_token;
    parameters[@"status"] = self.editTextView.text;
    parameters[@"visible"] = @{@1:@"type"};
    
    [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        UIImage * image = [self.picViewController.dataArray firstObject];
        
        NSData * data = UIImageJPEGRepresentation(image, 0.3);
        
        [formData appendPartWithFileData:data name:@"pic" fileName:@"pic.jpeg" mimeType:@"image/jpeg"];
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"success");
        [self dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error %@",error);
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    
    
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    self.picViewController = [segue destinationViewController];
    
    
    
}

@end
